package sk.ogame.realm.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sk.ogame.brain.Memory;
import sk.ogame.brain.thing.PlanetThing;
import sk.ogame.brain.thing.Thing;
import sk.ogame.brain.thing.ThingService;
import sk.ogame.entity.Planet;
import sk.ogame.entity.PlanetOverview;
import sk.ogame.realm.exception.NotSupportThingException;

import java.util.List;

/**
 * @author 支绍昆
 * 2023/2/15
 * 行星服务
 **/
@Service
public class PlanetService implements ThingService {

    private static final Logger logger = LoggerFactory.getLogger(PlanetService.class);

    @Autowired
    private Memory memory;
    @Autowired
    private ClientHttpService clientHttpService;

    @Override
    public Thing dealThing(Thing thing) {
        if (thing instanceof PlanetThing) {
            PlanetThing planetThing = (PlanetThing) thing;
            switch (planetThing.getOperate()) {
                case getInfo: {
                    int planetId = planetThing.getPlanetId();
                    if (!isCurrent(planetId)) {
                        change(planetId);
                    } else {
                        refreshInfo();
                    }
                    return null;
                }
                case getOverview: {
                    planetOverviewList();
                    return null;
                }
                default:
                    throw new NotSupportThingException(null, 0);
            }
        }
        throw new NotSupportThingException(null, 0);
    }

    public boolean isCurrent(int planetId) {
        return memory.getCurrentPlanet() == planetId;
    }

    public Planet change(int planetId) {
        return clientHttpService.planetChangeById(planetId);
    }

    public Planet refreshInfo() {
        return clientHttpService.planetRefresh();
    }

    public List<PlanetOverview> planetOverviewList() {
        return clientHttpService.planetOverviewList();
    }

}
